package tech.six6.service.impl;

import org.apache.ibatis.session.SqlSession;
import tech.six6.dao.MybatisDemoDao;
import tech.six6.entiy.MybatisDemo;
import tech.six6.service.MybatisDemoService;
import tech.six6.util.SqlsessionFactoryUtil;

import java.util.List;

public class MybatisDemoServiceImpl implements MybatisDemoService {

    SqlSession sqlSession = SqlsessionFactoryUtil.getSqlSessionFactory().openSession();

    MybatisDemoDao demoDao = sqlSession.getMapper(MybatisDemoDao.class);

    @Override
    public int add(MybatisDemo demo) {
        if (demo == null){
            return 0;
        }
        int i =demoDao.add(demo);
        sqlSession.commit();
        return i;
    }

    @Override
    public int delete(int id) {
        int i = demoDao.delete(id);
        sqlSession.commit();
        return i;
    }

    @Override
    public int update(MybatisDemo demo) {
        if (demo.getName()==null && demo.getPassword()==null && demo.getSex()==null){
            return 0;
        }
        int i = demoDao.update(demo);
        sqlSession.commit();
        return i;
    }

    @Override
    public MybatisDemo selectById(int id) {
        return demoDao.selectById(id);
    }

    @Override
    public List<MybatisDemo> selectAll() {
        return demoDao.selectAll();
    }
}
